const baseUrl = 'http://218.13.39.165:9999' // isgo项目地址
uni.setStorageSync('url', baseUrl)

function request(url, method, data, header) {
	if (url == '/Login/CheckAccount') {
		header = {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	} else {
		header = {
			'token': uni.getStorageSync('token'),
			'Content-Type': 'application/json'

		}
	}
	uni.showLoading({
		title: '加载中...'
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: method,
			data: data,
			header,
			success: function(res) {
				const {
					result,
					msg
				} = res.data
				if (result)
					resolve(res.data);
				else {
					uni.showToast({
						title: msg,
						icon: 'none'
					})
					reject()
				}
			},
			fail: function(res) {
				uni.showToast({
					title: '服务器加载失败！',
					icon: "none"
				})
				reject();
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}

// 封装get方法

function get(obj) {
	return request(obj.url, 'GET', obj.data)
}
// 封装post方法
function post(obj) {
	return request(obj.url, 'POST', obj.data)
}

export default {
	request,
	get,
	post
}